#include <iostream>
#include <string>
#include <sys/socket.h>
#include <arpa/inet.h>

int main()
{
    // 创建套接字
    int sockfd = ::socket(AF_INET, SOCK_DGRAM, 0);

    // 绑定网络信息
    sockaddr_in addr;
    addr.sin_family = AF_INET;
    addr.sin_port = htons(8080);
    addr.sin_addr.s_addr = inet_addr("0.0.0.0");

    ::bind(sockfd, (struct sockaddr*)&addr, sizeof(addr));

    int cnt = 0;
    struct sockaddr_in local;
    socklen_t len = sizeof(local);
    while (1)
    {
        std::cout << "第" << ++cnt << "次" << std::endl;
        // 读信息
        char buffer[1024] = {0};
        int n = recvfrom(sockfd, buffer, sizeof(buffer)-1, 0, (struct sockaddr*)&local, &len);
        
        // 回应
        std::cout << buffer << std::endl;
    }

    return 0;
}